package com.hotelManagerSystem.service.impl;

import com.hotelManagerSystem.model.Dish;
import com.hotelManagerSystem.mapper.DishMapper;
import com.hotelManagerSystem.service.IDishService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author treasy
 * @since 2021-12-22
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {
    @Autowired
    DishMapper dishMapper;

    @Override
    public Map<String, Object> queryAll() {
        Map<String, Object> map = new HashMap<>();
        //查询所有数据
        List<Dish> list = dishMapper.queryAll();
        map.put("dishList", list);
        return map;
    }

    @Override
    public Map<String, Object> queryByName(String name) {
        Map<String, Object> map = new HashMap<>();
        //传入空字符串查询全部
        List<Dish> list;
        if(name == "") {
            list = dishMapper.queryAll();
        } else {
            name = '%' + name + '%';
            list = dishMapper.queryByName(name);
        }
        map.put("dishList", list);
        return map;
    }

    @Override
    public Map<String, Object> addOne(Dish dish, MultipartFile photo) {
        Map<String, Object> ret = new HashMap<>();
        if (photo == null) {
            ret.put("type", 500);
            ret.put("msg", "选择要上传的文件！");
            return ret;
        }

        //获取文件后缀
        String suffix = photo.getOriginalFilename().substring(photo.getOriginalFilename().lastIndexOf(".") + 1, photo.getOriginalFilename().length());
        if (!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())) {
            ret.put("code", 200);
            ret.put("msg", "请选择jpg,jpeg,gif,png格式的图片！");
            return ret;
        }

        String staticPath = this.getClass().getClassLoader().getResource("static").getFile();
        try {
            staticPath = java.net.URLDecoder.decode(staticPath, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String savePath = staticPath  + "/dish/images/menu/";

        File savePathFile = new File(savePath);
        if(!savePathFile.exists()) {
            savePathFile.mkdir();
        }

        String filename = dish.getDishName() + ".jpg";

        int lastIndexOf = savePath.lastIndexOf("target");
        System.out.println(lastIndexOf);
        savePath = savePath.substring(0, lastIndexOf);
        savePath = savePath.substring(1) + "src/main/resources/static/dish/images/menu/";
        try {
            File f = new File(savePath + filename);
            //将文件保存指定目录
            photo.transferTo(f);
        } catch (Exception e) {
            ret.put("code", 500);
            ret.put("msg", "保存文件异常！");
            return ret;
        }

        ret.put("code", 200);
        ret.put("msg", "上传图片成功！");
        dish.setDishImg(filename);
        dishMapper.insert(dish);
        return ret;
    }
}
